<script type="text/javascript">
/*分类/地区等选择 共用*/
// 翻页
$("#tree_list").delegate(".pagination a","click",function(){
    $.get(this.href,function(html){
        $("#tree_list").replaceWith(html);
    });
    return false;
});

/*搜索
* url URL地址
*/
function tree_search(url){
    url = url + '&keyword=' + $('#keyword').val();
    $.get(url,function(html){
        $("#tree_list").replaceWith(html);
    });
}

/*关闭窗口
* id 赋值分类/地区等ID
* title 赋值分类/地区等标题
*/
// 关闭窗口
function check_close(id,title){
    $('#checkid').val(id);
    $('#tree_title').val(title);
    $("#modal").modal("hide");
}

/*点击展开子分类
* id ID
* title 标题
* tabledir 数据表
*/
function display_child(id,title,tabledir){
    var html = ''; var hide_id = ''; var tree = '';
    var parentid = '{:input('parentid')}';
    var sortid = '{:input('sortid')}';
    $.ajax({
        type: "POST",
        url: "{:url('common/child')}",
        dataType: "json",
        data: {tabledir:tabledir, id:id},
        success: function(data){
            $.each(data,function(i,val){
                tree = '';
                for(var j=0;j<val.level;j++){
                    tree += '|---';
                }
                html += '<tr id="tr_'+val.id+'">';
                html += '    <td>';
                html += '        <span class="d-invisible">'+val.id+'</span>';
                html += '    </td>';
                if(val.child == 'yes'){
                    html += '    <td id="td_'+val.id+'"><a onclick="javascript:display_child('+val.id+',\''+val.title+'\',\''+tabledir+'\');" href="javascript:void(0);">'+tree+val.title+'<i class="fa fa-plus-square fa-lg ml-2"></i></a></td>';
                }else{
                    html += '    <td>'+tree+val.title+'</td>';
                }
                html += '    <td>';
                if(parentid == val.id){
                    html += '    <a href="javascript:void(0);" onclick="javascript:check_close('+val.id+',\''+val.title+'\');" class="btn btn-success"><i class="fa fa-check-circle mr-2"></i> {:lang('v_select')}</a>';
                }else{
                    if(val.parentid != sortid && val.id != sortid){
                        html += '    <a href="javascript:void(0);" onclick="javascript:check_close('+val.id+',\''+val.title+'\');" class="btn btn-default">{:lang('v_select')}</a>';
                    }else{
                        html += '    {:lang('v_can_not')} {:lang('v_select')}';
                    }
                }
                html += '    </td>';
                html += '</tr>';
                hide_id = val.hide_id;
            });
            $('#tr_'+id+'').after(html);
            var parent_tree = tree.substring(0,tree.length-4);
            $('#td_'+id+'').html('<a onclick="javascript:hide_child('+id+',\''+hide_id+'\',\''+title+'\',\''+parent_tree+'\',\''+tabledir+'\');" href="javascript:void(0);">'+parent_tree+title+'<i class="fa fa-plus-square fa-lg ml-2"></i></a>');
        }
    });
}
</script>
{include file="public/js_hide_child" /}